package com.map.view;
import android.graphics.Canvas;
import android.graphics.Paint;

/*
地图图块层

*/


public class MapLayer implements Map
{

	

	private String name;
	private int width,height;
	private int blockWidth,blockHreight;
	private int data[];
	private boolean isVisible; //是否显示
	
	//创建地图层 宽度 高度 图块宽度 图块高度
	public MapLayer(int width,int height,int blockWidth,int blockHeight)
	{
	this.isVisible = true;
		this.width = width;
		this.height = height;
		this.blockWidth = blockWidth;
		this.blockHreight = blockHeight;
		this.data = new int[width*height];
	}
	
	public MapLayer(int width,int height,int blockWidth,int blockHeight,int data[])
	{
	this.isVisible = true;
		this.width = width;
		this.height = height;
		this.blockWidth = blockWidth;
		this.blockHreight = blockHeight;
		this.data = data;
	}
	//获取图层名字
	public String getName()
	{
		if(name==null)
			return "未命名";
		return this.name;
	}
	//设置图层名字
	public void setName(String name)
	{
		this.name = name;
	}
	//获取地图横向块数
  public int getWidth()
	{
		return this.width;
	}
	//获取地图纵向块数
	public int getHeight()
	{
		return this.height;
	}
	
	//设置动态修改地图宽度
	public int change(int width,int height)
	{
	int data[] = null;
	
	//int arr[] = data.clone();
	//将所有arr数组拷贝到新的data数组
	data = new int[width*height];
	//	data2 = new long[mapHeight][mapWidth];
	int w = Math.min(width, this.getWidth());
	int h = Math.min(height, this.getHeight());
	//复制
	for (int x=0;x<w;x++) {
	  for (int y=0;y < h;y++) {
	  data[y*width+x] = this.get(x,y);
	  }
	}
	this.data = data;
	this.width = width;
	this.height = height;
	
	return 0;
	}
	//动态修改地图高度
	
	//获取地图图块宽度
	public int getBlockWidth()
	{
		return this.blockWidth;
	}
	//获取地图图块高度
	public int getBlockHeight()
	{
		return this.blockHreight;
	}
	//获取地图指定位置的数值
	public int get(int index)
	{
		if(isVisible)
		return data[index];
		return 0;
	}
	
	public int get(int x,int y)
	{
		if(isVisible)
		return data[y*this.width+x];
		return 0;
	}
	
	//设置地图指定位置的数值
	public void set(int index, int num)
	{
		this.data[index]= num;
	}
	
	public void set(int x,int y,int num)
	{
		if(x>=0 && y>=0 && x<width && y<height)
		this.data[y*this.width+x]=num;
		else
			new ArrayIndexOutOfBoundsException();
	}
	
	//设置隐藏状态
	public void setVisible(boolean isVisible)
	{
		this.isVisible = isVisible;
	}
	
	//获取隐藏状态
	public boolean getVisible()
	{
		return this.isVisible;
	}
	
	//显示地图(数值模式)
	public void draw(Canvas canvas)
	{
		Paint paint = new Paint();
		for(int ix=0;ix<this.width;ix++)
		{
			for(int iy=0;iy<this.height;iy++)
			{
				canvas.drawText(""+data[iy*width+ix],ix*blockWidth,iy*blockHreight +paint.getTextSize(), paint);
			}
		}
		
	}
	 
	public int[] getData()
	{
		return this.data;
	}

	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		for(int iy=0;iy<height;iy++)
		{
			for(int ix=0;ix<width;ix++)
			{
				builder.append(""+get(ix,iy));
				builder.append(",");
			}
			builder.append("\n");
		}
		return builder.toString();
	}
	
	
	
}
